package com.action;

import com.entity.*;
import com.service.CoachService;
import com.service.CourseService;
import com.service.MuscleService;
import com.service.TorderService;
import com.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

import static com.opensymphony.xwork2.Action.SUCCESS;


@Controller
@Scope("prototype")
public class CourseAction extends BaseAction
{

    @Autowired
    private CourseService courseService;
    @Autowired
    private MuscleService muscleService;
    @Autowired
    private CoachService coachService;
    @Autowired
    private TorderService torderService;


    private Course course;
    private Muscle muscle;
    private PageBean pageBean=new PageBean();
    private Integer id;

    //作用域
    private List<Course> courses = new ArrayList<Course>();
    private List<Muscle> muscles = new ArrayList<Muscle>();
    private List<Muscle> muscles2 = new ArrayList<Muscle>();
    private List<Coach> coachs = new ArrayList<Coach>();

    
    public String list() 
    {
        String hql="from Course where 1=1 ";
        String hqlCount="select count(*) from Course where 1=1 ";
        pageBean.setTotalCount(courseService.getCount(hqlCount));
        courses = courseService.getAll(hql,pageBean.pageNo,pageBean.pageSize);
        String hql2="from Coach where 1=1 ";
        coachs = coachService.getAll(hql2,pageBean.pageNo,pageBean.pageSize);
        return "course";
    }

    public String add()
    {
        courseService.addCourse(course);
        return "list";
    }

    public String toupdate()
    {
       course=courseService.getCourseById(id);
        return SUCCESS;
    }

    public String doupdate()
    {
        courseService.updateCourse(course);
        return "list";
    }

    public String delete()
    {
        courseService.delById(id);
        return "list";
    }

    //课程前台遍历
    public String qlist() 
    {
        String hql="from Course where 1=1 ";
        User user= (User) request.getSession().getAttribute("user");
        courses = courseService.getByHql(hql);
        for(Course course:courses)
        {
            int c=(int)((Math.random()*30)+0);
            course.setSum(c);
        }
        if(user!=null)
        {
        for (int i=0;i<courses.size();i++)
        {
            Course course=courses.get(i);
            String hql2="from Torder where uid=? and cid=?";
            List<Torder> orders=torderService.getByHql(hql2,user.getId(),course.getId());
            if(orders.size()==0)
            {
                course.setJieguo(0);
            }
            else
            {
                Torder o=orders.get(0);
                if(o.getResult().equals("未通过"))
                {
                    course.setJieguo(1);
                }
                else
                {
                    course.setJieguo(2);
                }
            }

        }}
        String hql3="from Muscle where type='肌肉' ";
        muscles = muscleService.getAll(hql3,pageBean.pageNo,pageBean.pageSize);
        String hql4="from Muscle where type='骨骼' ";
        muscles2 = muscleService.getAll(hql4,pageBean.pageNo,pageBean.pageSize);
        return "index";
    }




    public CourseService getCourseService() 
    {
        return courseService;
    }

    public void setCourseService(CourseService courseService) 
    {
        this.courseService = courseService;
    }

    public Course getCourse() 
    {
        return course;
    }

    public void setCourse(Course course) 
    {
        this.course = course;
    }

    public PageBean getPageBean() 
    {
        return pageBean;
    }

    public void setPageBean(PageBean pageBean) 
    {
        this.pageBean = pageBean;
    }

    public List<Course> getCourses() 
    {
        return courses;
    }

    public void setCourses(List<Course> courses) 
    {
        this.courses = courses;
    }

    public Integer getId() 
    {
        return id;
    }

    public void setId(Integer id) 
    {
        this.id = id;
    }

    public Muscle getMuscle() 
    {
        return muscle;
    }

    public void setMuscle(Muscle muscle) 
    {
        this.muscle = muscle;
    }

    public List<Muscle> getMuscles() 
    {
        return muscles;
    }

    public void setMuscles(List<Muscle> muscles) 
    {
        this.muscles = muscles;
    }

    public List<Coach> getCoachs() 
    {
        return coachs;
    }

    public void setCoachs(List<Coach> coachs) 
    {
        this.coachs = coachs;
    }

    public List<Muscle> getMuscles2() 
    {
        return muscles2;
    }

    public void setMuscles2(List<Muscle> muscles2) 
    {
        this.muscles2 = muscles2;
    }
}
